package ejb.updater;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;

import org.jboss.ejb3.annotation.ResourceAdapter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

@ResourceAdapter("quartz-ra.rar")
@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName="cronTrigger", propertyValue="0 0-59/15 * * * ?")	
	}, messageListenerInterface=Job.class)
public class CurrentWeatherUpdater extends SchedWeatherUpdater {
	//"0 0-59/5 * * * ?"
	public CurrentWeatherUpdater() {
		super();
	}

	@Override
	public void execute(JobExecutionContext arg0) {
		boolean change = false;
		for(AltCity ct: cities) {
			System.out.print(ct.getName()+": ");
			XMLParserV2 parser = null;
			try {
				parser = new XMLParserV2(new URL(makeUrl(ct.getName())).openStream());
			} catch (MalformedURLException e) {
			// 	TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
			// 	TODO Auto-generated catch block
				e.printStackTrace();
			}
			int conds = parser.getCurrentConditions();
			int temp = parser.getCurrentTemp();
			double press = parser.getCurrentPressure();
			if(conds != ct.getConditions()  || 
					temp  != ct.getTemperature() ||
					press != ct.getPressure()    ){
				System.out.println("Update: " + ct.getName() + "[" + temp +", "+ press +", "+ conds +"]");
				change = true;
				ct.setConditions(conds);
				ct.setPressure((float) press);
				ct.setTemperature(temp);
				sendMessage(false, ct, false, 0);
			}
		}
		if(change) 
			sendMessage(true, null, false, 0);
	}
	
	
	
}
